package org.example.erpapispringweb.controller;



import org.example.erpapicommon.Util.ApiPageResult;
import org.example.erpapicommon.Util.ApiResult;
import org.example.erpapicommon.Util.Pageable;
import org.example.erpapicommon.Util.Param.*;
import org.example.erpapicommon.Util.ResponseType;
import org.example.erpapicommon.model.*;
import org.example.erpapicommon.service.OrderService;
import org.example.erpapicommon.service.OtherService;
import org.example.erpapicommon.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("/erp/api/v1")
public class ErpApiV1Controller {
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OtherService otherService;


    @GetMapping("/authors")
    public ResponseEntity<ApiResult<List<Author>>> author() {
        List<Author> authorList = new ArrayList<>();
        authorList.add(new Author("1", "张三", "1班"));
        ApiResult<List<Author>> apiResult = ApiResult.success(authorList);
        return ResponseEntity.ok(apiResult);
    }
    @PostMapping("/products")
    public ResponseEntity<ApiPageResult<TProduct>> products(@RequestBody ProductQueryParam param) {
        Pageable<TProduct> productPage = productService.PageProducts(param);
        ApiPageResult<TProduct> apiResult = ApiPageResult.success(productPage);
        return ResponseEntity.ok(apiResult);
    }

    @GetMapping("/product/{id}")
    public ResponseEntity<ApiResult<Product>> product(@PathVariable Integer id) {
        Optional<Product> productOptional=productService.getProductById(id);
        if (productOptional.isEmpty()) {
            return  ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(ApiResult.fail(ResponseType.NOT_FOUND,"未找到"));
        }
        Optional<ApiResult<Product>> optionalTProductApiResult = productOptional.map(ApiResult::success);
        return ResponseEntity.of(optionalTProductApiResult);
    }

    @PostMapping("/supplier/{id}/products")
    public ResponseEntity<ApiPageResult<TProduct>> productsBySupplierId(@PathVariable Integer id, @RequestBody ProductQueryParam param) {
        Pageable<TProduct> productPage = productService.PageProductsbySupplierId(param, id);
        ApiPageResult<TProduct> apiResult = ApiPageResult.success(productPage);
        return ResponseEntity.ok(apiResult);
    }

    @PostMapping("/orders")
    public ResponseEntity<ApiPageResult<TOrder>> orders(@RequestBody OrderQueryParam param) {
        Pageable<TOrder> orderPage = orderService.getOrders(param);
        ApiPageResult<TOrder> apiResult = ApiPageResult.success(orderPage);
        return ResponseEntity.ok(apiResult);
    }
    @GetMapping("/order/{id}")
    public ResponseEntity<ApiResult<Order>> order( @PathVariable String id) {
        Optional<Order> orderOptional = orderService.getOrdersById(id);
        if (orderOptional.isEmpty()) {
            return  ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(ApiResult.fail(ResponseType.NOT_FOUND,"未找到"));
        }
        Optional<ApiResult<Order>> optionalTOrderApiResult = orderOptional.map(ApiResult::success);
        return ResponseEntity.of(optionalTOrderApiResult);
    }
    @PostMapping("/employees")
    public ResponseEntity<ApiPageResult<TEmployee>> employees(@RequestBody EmployeeQueryParam param) {
        Pageable<TEmployee> employeePage = otherService.findEmployees(param);
        ApiPageResult<TEmployee> apiResult = ApiPageResult.success(employeePage);
        return ResponseEntity.ok(apiResult);
    }
    @PostMapping("/customers")
    public ResponseEntity<ApiPageResult<TCustomer>> customers(@RequestBody CustomerQueryParam param) {
        Pageable<TCustomer> customerPage = otherService.findCustomers(param);
        ApiPageResult<TCustomer> apiResult = ApiPageResult.success(customerPage);
        return ResponseEntity.ok(apiResult);
    }
    @PostMapping("/shippers")
    public ResponseEntity<ApiPageResult<TShipper>> shippers(@RequestBody ShipperQueryParam param) {
        Pageable<TShipper> shipperPage = otherService.findShippers(param);
        ApiPageResult<TShipper> apiResult = ApiPageResult.success(shipperPage);
        return ResponseEntity.ok(apiResult);
    }

    @PostMapping("/suppliers")
    public ResponseEntity<ApiPageResult<Supplier>> suppliers(@RequestBody SupplierQueryParam param) {
        Pageable<Supplier> supplierPage = otherService.findSuppliers(param);
        ApiPageResult<Supplier> apiResult = ApiPageResult.success(supplierPage);
        return ResponseEntity.ok(apiResult);
    }
    @GetMapping("/categories/level/{value}")
    public ResponseEntity<ApiResult<List<TCategory>>> categories(@PathVariable Integer value) {
        List<TCategory> categoryList = otherService.findCategoriesByLevel(value);
        ApiResult<List<TCategory>> apiResult = ApiResult.success(categoryList);
        return ResponseEntity.ok(apiResult);
    }
    @PostMapping("/stocks")
    public ResponseEntity<ApiPageResult<Stock>> stocks(@RequestBody PurchaseQueryParam param) {
        Pageable<Stock> stockPage = otherService.findPurchases(param);
        ApiPageResult<Stock> apiResult = ApiPageResult.success(stockPage);
        return ResponseEntity.ok(apiResult);
    }
}
